package com.ydl.shopping.controller.system;

import cn.hutool.core.util.StrUtil;
import com.ydl.shopping.annotation.Logger;
import com.ydl.shopping.core.controller.BaseController;
import com.ydl.shopping.core.vo.ResultVO;
import com.ydl.shopping.domain.TbUser;
import com.ydl.shopping.service.ITbUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户相关接口
 *
 * @author 林武泰
 */
@RestController
@CrossOrigin
@RequestMapping("/system/user")
@Tag(name = "用户Controller")
public class TbUserController extends BaseController {
    @Resource
    private ITbUserService service;

    @GetMapping("/me")
    @PreAuthorize("hasAnyAuthority('user:query')")
    @Operation(summary = "个人信息")
    public ResultVO me() {
        return success(service.me());
    }

    @DeleteMapping("/logout")
    @PreAuthorize("hasAnyAuthority('user:query')")
    @Operation(summary = "退出登陆")
    public ResultVO logout() {
        return service.logOut();
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('user:query')")
    @Operation(summary = "根据id查询用户")
    @Logger(model = "用户管理")
    public ResultVO getById(@PathVariable("id") Long id) {
        return success(service.getById(id));
    }

    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('user:query')")
    @Operation(summary = "查询用户列表")
    @Logger(model = "用户管理")
    public ResultVO list(@RequestBody TbUser entity) {
        // 获取页码
        Long pageNumber = entity.getPageNumber();
        // 获取页数
        Long pageSize = entity.getPageSize();
        // 开启分页
        startPage(pageNumber, pageSize);

        return getPageResult(service.list(service.queryWrapper(entity)));
    }

    // 127.0.0.1:8089/api/doc.html

    @PostMapping
    @PreAuthorize("hasAnyAuthority('user:save')")
    @Operation(summary = "新增用户")
    @Logger(model = "用户管理")
    public ResultVO save(@Validated @RequestBody TbUser entity) {
        entity.setCreateBy(username());
        String errorStr = service.saveUser(entity);
        return StrUtil.isNotBlank(errorStr) ? error(errorStr) : success();
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('user:update')")
    @Operation(summary = "修改用户")
    @Logger(model = "用户管理")
    public ResultVO update(@Validated @RequestBody TbUser entity) {
        entity.setUpdateBy(username());
        return toResult(service.updateUser(entity));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('user:delete')")
    @Operation(summary = "删除用户")
    @Logger(model = "用户管理")
    public ResultVO deleteById(@PathVariable("id") Long id) {
        return toResult(service.removeById(id));
    }

    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('user:delete')")
    @Operation(summary = "批量删除用户")
    @Logger(model = "用户管理")
    public ResultVO deleteByIds(@RequestParam List<Long> ids) {
        return toResult(service.removeByIds(ids));
    }
}
